/**
 * Created by duzi on 7/6/15.
 */
$(document).ready(function(){
    var bill = {
        $list: $('.returnitem .billItemList'),
        $billList: $('.returnitem .billList'),
        billId: 0,
        find: function(id){
            var self = this;
            var start = $('#start').val() || "2015-02-03";
            var end = $('#end').val() || "2015-07-20";
            $.ajax({
                type: "GET",
                url: "/bills/"  + id + '/' + start + '/' + end,
                contentType: 'application/json',
                success: function(response){
                    self.billView(response);
                }
            })
        },
        findBillItem: function(id){
            var self = this;
            $.ajax({
                type: "GET",
                url: "/bills/"  + id,
                contentType: 'application/json',
                success: function(response){
                    self.itemView(response);
                    self.billId = response.billId;
                }
            })
        },
        dele: function(id, $item){
            var self = this;
            $.ajax({
                type: "DELETE",
                url: "/billItems",
                data: JSON.stringify({items: [id]}),
                contentType: 'application/json',
                success: function(response){
                    alert('成功刪除');
                    $item.parent().remove();
                    self.findBillItem(self.billId);
                }
            });
        },
        billView: function(bill){
            this.$billList.empty();
            $('#billInfo').css('display','block');
            $('#billItemInfo').css('display','');
            var html = '';
            if(bill == 0) return;
            bill.forEach(function(info, index){
                var date = new Date(info.billCreatetime);
                var time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-'+ date.getDate();

                html += $('#tmple-bill').html()
                    .replace('{id}', info.billId)
                    .replace('{id}', info.billId)
                    .replace('{name}', info.customer.name)
                    .replace('{user}', info.user.userName)
                    .replace("{time}", time);

            });
            this.$billList.append(html);
        },
        itemView: function(bill){
            this.$list.empty();
            $('#billItemInfo').css('display','block');
            var html = '';
            $('#customerId').html(bill.customer.customerId);
            $('#userId').html(bill.user.userId);
            bill.billItems.forEach(function(info, index){
                if(info.billItemStatus == 1){
                    html += $('#tmple-listItem').html().replace('{id}', info.product.productId)
                        .replace('{name}', info.product.productName)
                        .replace('{sum}', info.billItemSum)
                        .replace("{sumPrice}", info.billItemSumPrice)
                        .replace("{listLength}", info.billItemId);
                }

            });

           this.$list.append(html);
        },
        event: function(){
            var self = this;
            $('#billItemDelete').on('keyup', function(){
                this.value = this.value.replace(/[^\d]/g,'');
                if(!this.value){
                    return;
                }
                self.find(this.value);
            });

            self.$list.on('click', '.delete-right', function(){
                var id = $(this).data('id');
                self.dele(id, $(this));
            });

            self.$billList.on('click', 'li', function(){
                $('#billInfo').css('display', '');
                self.findBillItem($(this).data('id'));
            })
        }
    }

    bill.event();
})